/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view;

import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.Element;

public class Heading implements Element {
	Element element;
	String content;
	
	public Heading(Element element) {
		super();
		try {
			this.element = element;
			this.content = element.getDocument().getText(element.getStartOffset(), element.getEndOffset()-element.getStartOffset());
		}
		catch( Exception e) {
			e.printStackTrace();
		}
	}
	
	public boolean isModified() {
		try {
			if ( !content.equals( element.getDocument().getText(element.getStartOffset(), element.getEndOffset()-element.getStartOffset()) )) {
				content = element.getDocument().getText(element.getStartOffset(), element.getEndOffset()-element.getStartOffset());
				return true;
			}
		}
		catch( Exception e) {
			e.printStackTrace();
		}
		
		
		return false;
	}

	public Element getElement() {
		return element;
	}

	public AttributeSet getAttributes() {
		return element.getAttributes();
	}

	public Document getDocument() {
		return element.getDocument();
	}

	public Element getElement(int index) {
		return element.getElement(index);
	}

	public int getElementCount() {
		return element.getElementCount();
	}

	public int getElementIndex(int offset) {
		return element.getElementIndex(offset);
	}

	public int getEndOffset() {
		return element.getEndOffset();
	}

	public String getName() {
		return element.getName();
	}

	public Element getParentElement() {
		return element.getParentElement();
	}

	public int getStartOffset() {
		return element.getStartOffset();
	}

	public boolean isLeaf() {
		return element.isLeaf();
	}
	
	public String toString() {
		return content;
	}
	
	public boolean equals( Object e) {
		if ( !(e instanceof Heading))
			return false;
		return content.equals(e.toString());
	}
}

